#!/bin/bash
#
# Startup script for the lesskeeper
#
# chkconfig:   345 85 15
# description: lesskeeper
# processname: lesskeeper
# config:

# Source function library.
. /etc/rc.d/init.d/functions


DAEMON=/opt/less/keeper/bin/lesskeeper
DAEMON_ARGS="--prefix=/opt/less/keeper"
NAME=lesskeeper
NAMESTORE=lesskeeper-store
APPUSER=root

set -e

case "$1" in
    start)
        echo -n -e "Starting $NAME ...\n" 
        
        if pidof $NAME; then
            echo -n -e "Started\n"
            exit 0
        fi
        
        if pidof $NAMESTORE; then
            kill -9 `pidof $NAMESTORE`
            sleep 1
        fi
        
        # Start lesskeeper!
        $DAEMON $DAEMON_ARGS > /dev/null 2>&1 &
        sleep 1
        echo -n -e "$NAME PID: "
        if pidof $NAME; then
            echo -n -e "Started\n"
        else
            echo -n -e "Failed\n"
        fi
        ;;

    stop)

        echo -n -e "Stopping $NAME ...\n"
        echo -n -e "$NAME PID: "

        if pidof $NAME; then
            kill -9 `pidof $NAME`
            sleep 1
        fi
        
        if pidof $NAMESTORE; then
            kill -9 `pidof $NAMESTORE`
            sleep 1
        fi

        echo -n -e "\nStopped\n"
        ;;

    restart)
        ${0} stop
        ${0} start
        ;;

    status)
        echo -n -e "$NAME PID: "
        if pidof -s $NAME; then
            echo -n -e "\n$NAME is running\n"
        else
            echo -n -e "\n$NAME is not running\n"
        fi
        ;;

    *)
        echo "Usage: /etc/init.d/$NAME {start|stop|restart|status}" >&2
        exit 1
        ;;
esac

exit 0
